package com.zw.pm.module.system.user;


import com.zw.pm.constant.Constant;
import com.zw.pm.gen.entity.User;
import com.zw.pm.gen.mapper.UserMapper;
import com.zw.pm.module.integrate.interactive.MessageModel;
import com.zw.pm.util.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.session.Session;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.zw.pm.constant.Constant.STATUS_NORMAL;

/**
 * @Description: 用户 服务层
 *
 * @Author: China.sgl
 * @Date: 2019/9/16 09:11
 */
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserDao userDao;

    /**
     * 根据条件查询用户
     *
     * @param user 查询条件
     * @return 用户表数据
     */
    public List<User> findAll(User user){
        return userDao.findAll(user);
    }

    /**
     * 按帐号查询用户
     *
     * @param account 帐号
     * @return 用户数据
     */
    public User findByAccount(String account){
        return userDao.findByAccount(account);
    }

    /**
     * 修改帐户
     *
     * @param user 帐户对象
     * @return 影响行数
     */
    public int updateUser(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    /**
     * 重置密码
     *
     * @param id 帐户 ID
     * @return 影响行数
     */
    public int resetPassword(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        Object obj = new SimpleHash("MD5", "111111", ByteSource.Util.bytes(user.getAccount()), 8);
        user.setPassword(obj.toString());
        return updateUser(user);
    }

    /**
     * 保存帐户及帐户拥有角色
     *
     * @param user 帐户信息
     * @param roleIds 角色 ID 数组
     * @return 影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    public int saveUser(User user, Integer[] roleIds) {
        Object obj = new SimpleHash("MD5", user.getPassword(), ByteSource.Util.bytes(user.getAccount()), 8);
        user.setPassword(obj.toString());
        user.setStatus(STATUS_NORMAL);
        int row = userMapper.insert(user);
        if (roleIds.length > 0) {
            userDao.batchInsertUserRoles(user.getId(), roleIds);
        }
        return row;
    }

    /**
     * 修改帐户及拥有角色
     *
     * @param user 帐户信息
     * @param roleIds 拥有角色ID
     * @return 影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateUser(User user, Integer[] roleIds) {
        int row = userMapper.updateByPrimaryKeySelective(user);
        userDao.deleteUserRoleByUserId(user.getId());
        if (roleIds.length > 0) {
            userDao.batchInsertUserRoles(user.getId(), roleIds);
        }
        return row;
    }

    /**
     * 修改密码
     *
     * @param password 密码
     * @return 影响行数
     */
    public int updatePassword(String password) {
        Session session = SecurityUtils.getSubject().getSession();
        User user = (User) session.getAttribute("user");
        int hashIterations = 8;
        Object obj = new SimpleHash("MD5", password, ByteSource.Util.bytes(user.getAccount()), hashIterations);
        user.setPassword(obj.toString());
        return updateUser(user);
    }

    /**
     * 根据ID查询用户
     *
     * @param id 用户ID
     * @return 用户
     */
    public User findUserById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    /**
     * 修改密码
     *
     * @param password 密码
     * @return 行数
     */
    public int updateUserPassword(String password) {
        User user = ShiroUtils.getUser();
        int hashIterations = 8;
        Object obj = new SimpleHash("MD5", password, ByteSource.Util.bytes(user.getAccount()), hashIterations);
        user.setPassword(obj.toString());
        return userMapper.updateByPrimaryKey(user);
    }

    /**
     * 获取用户未读消息
     *
     * @param type 消息类型
     * @return list
     */
    public List<MessageModel> findMessageByType(Integer type) {
        return userDao.findMessageByUserId(ShiroUtils.getUserId(), type);
    }

    /**
     * 标记互动消息为已读
     */
    public void markInteractive() {
        userDao.updateMessageStatusByUserAndType(ShiroUtils.getUserId(), Constant.MESSAGE_TYPE_INTERACTIVE);
    }

    /**
     * 标记任务下达消息为已读
     */
    public void markSend() {
        userDao.updateMessageStatusByUserAndType(ShiroUtils.getUserId(), Constant.MESSAGE_TYPE_SEND);
    }

    /**
     * 标记项目变更消息为已读
     */
    public void markChange() {
        userDao.updateMessageStatusByUserAndType(ShiroUtils.getUserId(), Constant.MESSAGE_TYPE_CHANGE);
    }
}
